<!--20210421  zaq  新建修改密码界面-->
<!--20210510  zaq  调整按钮位置-->
<!--20210519  zaq  与后端进行密码修改调试-->
<!--20210520  zaq  修改密码调试完成-->

<template>
<div class="update_pwd">
  <el-form :model="ruleForm" status-icon :rules="check" ref="ruleForm" label-width="100px">
    <el-form-item label="原密码" prop="checkPwd">
      <el-input  style="width: 90%" type="password" v-model="ruleForm.ex_pwd"></el-input>
    </el-form-item>

    <el-form-item label="新密码" prop="pass">
      <el-input style="width: 90%" type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>

    <el-form-item label="确认密码" prop="checkPass">
      <el-input style="width: 90%" type="password" v-model="ruleForm.checkPass"
                autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="update('ruleForm')">确定</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>

  </el-form>

  <!--    成功信息对话框-->
  <el-dialog
      title="提示"
      v-model="successDialog.visible"
      width="30%"
      :before-close="exit">

    <el-result icon="success" title="密码修改成功" :subTitle="successDialog.message"/>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="exit">确 定</el-button>
        </span>
    </template>
  </el-dialog>

  <!--    失败信息对话框-->
  <el-dialog
      title="提示"
      v-model="failDialog.visible"
      width="30%"
      :before-close="handleClose">

    <el-result icon="error" title="密码修改失败" :subTitle="failDialog.message"/>

    <template #footer>
        <span class="dialog-footer">
          <el-button @click="failDialog.visible = false">取 消</el-button>
          <el-button type="primary" @click="failDialog.visible = false">确 定</el-button>
        </span>
    </template>
  </el-dialog>


</div>
</template>

<script>
import { ElMessage } from 'element-plus';
import {updatePwd_11302} from "../../api";


export default {
  name: "Update_pwd",
  props:{
  },
  data(){
    // 合法性判断
    var checkPwd = (rule, value, callback) => {
      //合法性判断
      if (!value) {
        return callback(new Error('密码不能为空'));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      // 成功信息
      successDialog:{
        visible:false,
        message:'',
      },
      // 失败信息
      failDialog:{
        visible:false,
        message:'',
      },
      ruleForm:{
        pass:'',
        checkPass:'',
        ex_pwd:''
      },
      check: {
        pass: [
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
          {validator: validatePass2, trigger: 'blur'}
        ],
        ex_pwd: [
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
          {validator: checkPwd, trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    exit(done) {
      //密码修改成功，退出登录
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
            // 退出
            sessionStorage.setItem("uId",null)
            this.$store.commit("userStatus",null,null)
            this.$router.push('/')
          })
          .catch(_ => {
          });
    },
    update(ruleForm){
      const _this=this;
      var uId=sessionStorage.getItem("uId")
      console.log(this.ruleForm.pass)
      // 验证密码合法性
      this.$refs[ruleForm].validate((valid) => {
        if (!valid) {
          ElMessage.warning({
            message: '填写信息不符合规范，请重试',
            type: 'warning'
          });
          return false;
        } else {
          var body = {
            code: -1,
            note: '',
            uId: uId,
            exPwd: this.ruleForm.ex_pwd,
            pwd: this.ruleForm.pass
          }

          //调用接口11302
          updatePwd_11302(body).then(resp=>{
            if(resp.code>0){
              //弹出成功提示
              _this.successDialog.message=resp.note;
              _this.successDialog.visible=true;



            }else{
              //弹出失败信息
              _this.failDialog.message=resp.note;
              _this.failDialog.visible=true;
            }
          }).catch(error => {
            console.log('调用接口11302失败：' + error);
          });
        }
      });
    },//更新密码
    resetForm(ruleForm){
      this.$refs[ruleForm].resetFields();
    },//重置
  }
}
</script>

<style scoped>

</style>